/*
 * Copyright 2012 Alain Pannetrat (L1L1@gmx.com)
 * 
 * This file is part of cookie-miner.
 * 
 * cookie-miner is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * cookie-miner is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with cookie-miner. If not, see http://www.gnu.org/licenses/.
 *
 * @version: $Id$
 *
 */
#ifndef _HTTP_SERVER_H_
#define _HTTP_SERVER_H_

#include "http_packet.h"
#include "bufio.h"
#include "common.h"
#include "json.h"
#include "url.h"

class HttpServer : private Uncopyable { 
	public:
		HttpServer(Json::Value *config);

		HttpServer(unsigned short port=8081, const char *root_path=NULL);

		virtual ~HttpServer();

		bool run();
		
		virtual bool dispatch_request(BufferInOutSocket& bio, const HttpPacket& request);

		virtual bool do_get(BufferInOutSocket& bio, const HttpPacket& request);
		
		virtual bool do_head(BufferInOutSocket& bio, const HttpPacket& request);

		virtual bool send_error(BufferInOutSocket& bio, int errcode) const;

		virtual const char* server_name() const { return "http_server_cc/0.1"; }

		const char *root() const { return _server_root; }
	protected:
		bool do_default_content(BufferInOutSocket& bio,
			       	        const HttpPacket& request,	
				        bool include_content) const;

		bool do_default_error(BufferInOutSocket& bio,
				      int errcode,
				      bool include_content, 
				      const std::string& extra_content) const;
	private:
		char _server_root[PATH_MAX];
		unsigned short _server_port;

};

#endif
